const Router = require('koa-router')
const moment = require('moment')
const ArticleCommon = require('../common/article')
const ArticleModel = require('../models/article')

const route = new Router()

// 首页
route.get('/', async (ctx) => {
    let data = {
        isLogin: ctx.session.user === true,
        title: '程序员的博客',
        articleList: []
    }
    data.articleList = await ArticleCommon.find()
    await ctx.render('index', data)
})
// 登录

route.get('/login', async (ctx) => {
    let data = {
        title: '登录',
        isLogin: false,
        msg: ''
    }
    await ctx.render('login', data)
})

// 文章详情
route.get('/articleInfo/:id', async (ctx) => {
    let data = {
        isLogin: ctx.session.user === true,
        title: '文章详情'
    }
    const { id } = ctx.params
    data.article = await ArticleModel.findById(id)
    data.article.time = moment(data.article.createTime).format('YYYY-MM-DD')
    await ctx.render('article-info', data)
})


route.get('/category', async (ctx) => {
    let data = {
        isLogin: ctx.session.user === true,
        title: '文章详情'
    }
    await ctx.render('article-category', data)
})

module.exports = route
